package org.dromara.nursing.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.nursing.domain.vo.VisitorVo;
import org.dromara.nursing.domain.bo.VisitorBo;
import org.dromara.nursing.service.IVisitorService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 访客记录
 *
 * @author Xue
 * @date 2025-03-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/nursing/visitor")
public class VisitorController extends BaseController {

    private final IVisitorService visitorService;

    /**
     * 查询访客记录列表
     */
    // @SaCheckPermission("nursing:visitor:list") // 已移除权限验证
    @GetMapping("/list")
    public TableDataInfo<VisitorVo> list(VisitorBo bo, PageQuery pageQuery) {
        return visitorService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出访客记录列表
     */
    // @SaCheckPermission("nursing:visitor:export") // 已移除权限验证
    @Log(title = "访客记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(VisitorBo bo, HttpServletResponse response) {
        List<VisitorVo> list = visitorService.queryList(bo);
        ExcelUtil.exportExcel(list, "访客记录", VisitorVo.class, response);
    }

    /**
     * 获取访客记录详细信息
     *
     * @param visitorId 主键
     */
    // @SaCheckPermission("nursing:visitor:query") // 已移除权限验证
    @GetMapping("/{visitorId}")
    public R<VisitorVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long visitorId) {
        return R.ok(visitorService.queryById(visitorId));
    }

    /**
     * 新增访客记录
     */
    // @SaCheckPermission("nursing:visitor:add") // 已移除权限验证
    @Log(title = "访客记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VisitorBo bo) {
        return toAjax(visitorService.insertByBo(bo));
    }

    /**
     * 修改访客记录
     */
    // @SaCheckPermission("nursing:visitor:edit") // 已移除权限验证
    @Log(title = "访客记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VisitorBo bo) {
        return toAjax(visitorService.updateByBo(bo));
    }

    /**
     * 删除访客记录
     *
     * @param visitorIds 主键串
     */
    // @SaCheckPermission("nursing:visitor:remove") // 已移除权限验证
    @Log(title = "访客记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{visitorIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] visitorIds) {
        return toAjax(visitorService.deleteWithValidByIds(List.of(visitorIds), true));
    }
}
